﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DapperExtensions;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.Helper;
using Ext.Net;

namespace ExaminationSys.View.News
{
    public partial class NewsTypeEdit : System.Web.UI.UserControl
    {
        private static readonly NewsTypeService Proxy = ObjectFactory<NewsTypeService>.Create();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Store GridStore { get; set; }
        public string ID { get; set; }
        public void SetData(string name, string id, string desc)
        {
            NewsTypeName.Text = name;
            NewsTypeDesctiption.Text = desc;
            ID = id;
            X.Js.Call("setID", id);
        }
        public void Show()
        {
            this.NewsTypeEditWindow.Show();
        }
        protected void SaveNewsType(object sender, DirectEventArgs e)
        {
            var name = NewsTypeName.Text.Trim();
            if (string.IsNullOrWhiteSpace(name))
            {
                MessageBoxHelper.ShowWarning("名称不能为空");
                return;
            }
            var id = TypeIDHidden.Value;
            var desc = NewsTypeDesctiption.Text;
            Guid realId = string.IsNullOrEmpty(id) ? Guid.NewGuid() : new Guid(id);
            if (Proxy.CheckNameExists(name, realId))
            {
                MessageBoxHelper.ShowWarning(string.Format("名称 {0} 已经存在", name));
                return;

            }
            var sucess = false;
            int status=Status.Checked ? 1 : 0;
            if (!string.IsNullOrEmpty(id))
            {
                var entity = Proxy.Get(realId);
                entity.Name = name;
                entity.Status = status;
                entity.Description = desc;

                var op = FormsAuthenticationHelper.GetCurrentUserID();
                if (string.IsNullOrEmpty(op))
                {
                    FormsAuthenticationHelper.SignOut();
                    Response.Redirect("~/Account/Login.aspx");
                }
                entity.Opeator = op;

                entity.OpeatorTime = DateTime.Now;

                //update
                sucess = Proxy.Update(entity);
            }
            else
            {
                //insert
                var entity = new NewsType();
                entity.ID = realId;
                entity.Name = name;
                entity.Status =status;
                entity.Description = desc;

                var op = FormsAuthenticationHelper.GetCurrentUserID();
                if (string.IsNullOrEmpty(op))
                {
                    FormsAuthenticationHelper.SignOut();
                    Response.Redirect("~/Account/Login.aspx");
                }
                entity.Opeator = op;

                entity.OpeatorTime = DateTime.Now;
                var retCode = Proxy.Insert(entity);
                sucess = retCode != null;
            }
            if (sucess)
            {
                MessageBoxHelper.ShowInfo("保存成功");
                GridStore.Reload();
                NewsTypeEditWindow.Hide();
            }
            else
            {
                MessageBoxHelper.ShowInfo("保存失败");
                return;

            }
        }
    }
}